package com.wust.demo1.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;

public class CheckCodeImage {
	private Integer width;
	private Integer height;
	private Integer frontColor;
	private Integer backgroundColor;
	private BufferedImage image;
	
	public Color getRandColor(int fc,int bc){
		Random random = new Random();
		if(fc > 255) fc = 255;
		if(bc > 255) bc = 255;
		int r = fc + random.nextInt(bc-fc);
		int g = fc + random.nextInt(bc-fc);
		int b = fc + random.nextInt(bc-fc);
		return new Color(r,g,b);
	}
	
	private BufferedImage createImage(int width,int height){
		BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
		return image;
	}
	
	private void createCheckCodeImage(){
		int width = 60;
		int height = 20;
		//在内存中创建图像
		BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
		//获取图形上下文
		Graphics g = image.getGraphics();
		
		Random random = new Random();
		//设定背景色
		g.setColor(this.getRandColor(200, 259));
		g.fillRect(0,0,width,height);
		
		//设定字体
		g.setFont(new Font("Times New Roman",Font.PLAIN,18));
		
		//画边框
		//g.setColor(new Color())
		//g.drawRect(0,0,widht-1,height-1);
		
		g.setColor(this.getRandColor(169, 200));
		for(int i = 0; i < 155; i++){
			int x = random.nextInt(width);
			int y = random.nextInt(height);
			int x1 = random.nextInt(12);
			int y1 = random.nextInt(12);
			g.drawLine(x, y, x+x1, y+y1);
		}
		//取随机产生的认证码(4位)
		//String rand = request.getParameter("rand");
		//rand = rand.substring(0,rand.indexOf("."));
		String sRand = "";
		for(int i = 0; i< 4; i++){
			String rand = String.valueOf(random.nextInt(10));
			sRand += rand;
			g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
			g.drawString(rand, 13*i+6, 16);
			g.dispose();
		}
	}
	public Integer getWidth() {
		return width;
	}
	public void setWidth(Integer width) {
		this.width = width;
	}
	public Integer getHeight() {
		return height;
	}
	public void setHeight(Integer height) {
		this.height = height;
	}
	public Integer getFrontColor() {
		return frontColor;
	}
	public void setFrontColor(Integer frontColor) {
		this.frontColor = frontColor;
	}
	public Integer getBackgroundColor() {
		return backgroundColor;
	}
	public void setBackgroundColor(Integer backgroundColor) {
		this.backgroundColor = backgroundColor;
	}
	public BufferedImage getImage() {
		return image;
	}
	public void setImage(BufferedImage image) {
		this.image = image;
	}
	
}
